/*
脚本说明:批量删除用户或角色所分配的权限。
程序员:
*/

//脚本唯一关键字,通过该关键字找到对应脚本，每个脚本必须有该描述。
var v_script_id = 'authall_del';
//脚本状态,0:该脚本不许执行，1:该脚本可以执行
var v_status = '1';

//脚本逻辑写在run语句之间
if (run  && action.getAuthority().is(5,"9")){
	var err = '';
	var sql = '';
	var checkids = action.form.getString('checkids_allAuth');
	var arr_checkids = checkids.split(',');
	action.errorCode = 0;
	var t0=false,t1=false,t2=false,t3=false;
	for(i=0; i<arr_checkids.length; i++){
		var _ids = arr_checkids[i].split('-');
		var tname = _ids[0];
		var id = _ids[1];
		if(tname=='t0'){
			//删除h_auth_role_infos
			sql = 'delete from h_auth_role_infos where id = ' + id;
			t0=true;
		}else if(tname=='t1'){
			//删除h_auth_dyna_role_infos
			sql = 'delete from h_auth_dyna_role_infos where id = ' + id;
			t1=true;
		}else if(tname=='t2'){
			//删除h_auth_user_infos
			sql = 'delete from h_auth_user_infos where id = ' + id;
			t2=true;
		}else if(tname=='t3'){
			//删除h_auth_dyna_user_infos
			sql = 'delete from h_auth_dyna_user_infos where id = ' + id;
			t3=true;
		}
		if(action.dbUtil.update("",sql)==0) 
		{
			action.errorCode = 3;//数据库操作失败
			//action.retString = '数据库操作失败';//错误文字说明
			if( ''==err ){
				err += arr_checkids[i];
			}else{
				err += ',' + arr_checkids[i];
			} 
		}
	}
	
	if(action.errorCode != 0) action.retString = '以下ID删除失败: ' + err;
	
	//刷新权限分配缓存
	if (action.errorCode ==0){
		if (t0) { com.handy.plugin.auth.TAuthCache.getInstance().loadAuthRoleInfos(); }
		if (t1) { com.handy.plugin.auth.TAuthCache.getInstance().loadAuthDynaRoleInfos(); }
		if (t2) { com.handy.plugin.auth.TAuthCache.getInstance().loadAuthUserInfos(); }
		if (t3) { com.handy.plugin.auth.TAuthCache.getInstance().loadAuthDynaUserInfos(); }
	}
}